package edu.ccut.titan.component.impl.directory;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

import org.apache.commons.io.FileUtils;
import org.osgi.framework.Bundle;

import edu.ccut.titan.component.IBundleInfo;
import edu.ccut.titan.component.TitanComponentException;

public class TitanWebDirectoryInfo extends AbstractDirectoryInfo {

	public TitanWebDirectoryInfo() {
		super(TITAN_DIRECTORY_TYPE_WEB);
	}


	@Override
	public void load(Bundle bundle, IBundleInfo bundleInfo)
			throws TitanComponentException {

		this.deploy(bundle, super.getDirectoryPath(), this
				.getWebRootBundleDirectory(bundleInfo.getWebRootPath(), bundle.getSymbolicName()));
	}

	@Override
	public void unload(Bundle bundle, IBundleInfo bundleInfo)
			throws TitanComponentException {
		
		this.undeploy(this.getWebRootBundleDirectory(bundleInfo.getWebRootPath(), bundle.getSymbolicName()));
	}

	private String getWebRootBundleDirectory(String webRootPath, String symbolicName)
			throws TitanComponentException {
		if (webRootPath == null || webRootPath.length() == 0) {
			throw new TitanComponentException("Can't find web root path");
		}

		String webRootBundleDirectory = webRootPath.replace("\\", "/")
				+ "/" + symbolicName;

		return webRootBundleDirectory;
	}

	@SuppressWarnings("unchecked")
	private void deploy(Bundle bundle, String directoryPath,
			String webRootBundlePath) throws TitanComponentException {

		createDirctory(webRootBundlePath);

		copyDirectory(bundle, directoryPath, webRootBundlePath);
	}

	private void undeploy(String webRootBundlePath)
			throws TitanComponentException {
		
		if (webRootBundlePath != null) {
			File directory = new File(webRootBundlePath);
			try {
				FileUtils.deleteDirectory(directory);
			} catch (IOException e) {
				throw new TitanComponentException(e);
			}
		}

		// TODO:是否有更好的方案，现在没有删除容器的jsp缓存
	}

	@SuppressWarnings("unchecked")
	private void copyDirectory(Bundle bundle, String directoryPath,
			String copyPath) throws TitanComponentException {

		createDirctory(copyPath + "/" + directoryPath);

		Enumeration<String> filePaths = bundle.getEntryPaths(directoryPath);

		if (filePaths == null) {
			return;
		}

		while (filePaths.hasMoreElements()) {
			String filePath = filePaths.nextElement();

			if (filePath.endsWith("/")) {
				copyDirectory(bundle, filePath, copyPath);
			} else {
				createFile(bundle, copyPath + "/" + filePath, filePath);
			}
		}
	}

	private void createDirctory(String path) throws TitanComponentException {
		File directory = new File(path);

		if (directory.exists()) {
			try {
				FileUtils.deleteDirectory(directory);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		if (!directory.mkdirs()) {
			throw new TitanComponentException("create direcory failed in ["
					+ path + "]");
		}
	}

	private void createFile(Bundle bundle, String copyFilePath, String filePath)
			throws TitanComponentException {
		try {
			File file = new File(copyFilePath);

			URL url = bundle.getEntry(filePath);

			FileUtils.copyURLToFile(url, file);
		} catch (IOException e) {
			throw new TitanComponentException("create file failed in ["
					+ copyFilePath + "]");
		}
	}
}
